package com.ruoyi.mateproduction.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateDaily;
import com.ruoyi.mateproduction.domain.MateDailyModel;
import com.ruoyi.mateproduction.mapper.MateDailyMapper;
import com.ruoyi.mateproduction.mapper.MtRecordMapper;
import com.ruoyi.mateproduction.service.MateDailyService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateDailyServiceImpl implements MateDailyService {

    @Autowired
    MateDailyMapper mateDailyMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MtRecordMapper mtRecordMapper;

    @Override
    public int addMateDaily(MateDaily mateDaily) {

        int num = 0;
        mateDaily.setDeptId(SecurityUtils.getDeptId());
        Map<String, Object> resMap = new HashMap<>();
        try {
            num = mateDailyMapper.addMateDaily(mateDaily);
            if (mateDaily.getMateDailyModels() != null) {
                for (MateDailyModel mateDailyModel : mateDaily.getMateDailyModels()) {
                    mateDailyModel.setDailyId(mateDaily.getDailyId());
                    mateDailyModel.setDeptId(SecurityUtils.getDeptId());
                    mateDailyMapper.addMateDailyModel(mateDailyModel);
                }
                MtAudit mtAudit = new MtAudit();
                mtAudit.setCompanyId(SecurityUtils.getDeptId());
                mtAudit.setOrderId(mateDaily.getDailyId());
                mtAudit.setZjName(mateDaily.getAuditName());
                mtAudit.setOrderType("9");
                mtAudit.setOrderNum(mateDaily.getDailyOrderNum());
                mtAudit.setAuditStatus("0");
                mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit);

            }
        } catch (Exception e) {
            mateDailyMapper.removeMateDaily(mateDaily.getDailyId());
            resMap.put("orderId", mateDaily.getDailyId());
            resMap.put("orderType", "9");
            mtAuditMapper.deleteMtAuditById(resMap);

        }
        return num;
    }

    @Override
    public List<MateDaily> dailyList(MateDaily mateDaily) {
        return mateDailyMapper.dailyList(mateDaily);
    }

    @Override
    public MateDaily selectMateDaily(Long dailyId) {
        return mateDailyMapper.selectMateDaily(dailyId);
    }

    @Override
    public void removeDaily(Long dailyId) {
        Map<String, Object> resMap = new HashMap<>();
        mateDailyMapper.removeMateDaily(dailyId);
        resMap.put("orderId", dailyId);
        resMap.put("orderType", "9");
        mtAuditMapper.deleteMtAuditById(resMap);
    }

    @Override
    public int editDaily(MateDaily mateDaily) {
        int num = 0;
        if (mateDaily.getMateDailyModels() != null) {
            mateDailyMapper.removeMateDailyModel(mateDaily.getDailyId());
            for (MateDailyModel mateDailyModel : mateDaily.getMateDailyModels()) {
                mateDailyModel.setDeptId(SecurityUtils.getDeptId());
                mateDailyModel.setDailyId(mateDaily.getDailyId());
                mateDailyMapper.addMateDailyModel(mateDailyModel);

            }

        }
        num = mateDailyMapper.editMatedaily(mateDaily);

        return num;
    }

    @Override
    public List<MateDaily> auditDaily(Long deptId) {
        return mateDailyMapper.auditDaily(deptId);
    }
}
